// pages/save/save.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemlist: [],
    title: null,
    fc: null,
    writer: null,
    theme: null,
    titleshow: false,
    writershow: false,
    Img: [],
    swiperIndex: 0
  },
  showTitle: function (e) {
    this.setData({
      titleshow: !this.data.titleshow
    })
  },
  showWriter: function (e) {
    this.setData({
      writershow: !this.data.writershow
    })
  },
  generate: function () {
    let t = ''
    let w = ''
    if (!this.data.titleshow) {
      t = this.data.title
    }
    if (!this.data.writershow) {
      w = this.data.writer
    }
    wx.navigateTo({
      url: '../canvas/canvas?theme=' + this.data.theme + '&swiperIndex=' + this.data.swiperIndex + '&fc=' + this.data.fc + '&title=' + t + '&writer=' + w + '&itemlist=' + this.data.itemlist,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var fc
    if (options.theme == "blue") {
      fc = "#fff"
    }
    else if (options.theme == "white") {
      fc = "#002FA7"
    }

    let img = []
    const db = wx.cloud.database()
    db.collection('bg').where({
      theme: options.theme
    }).get({
      success: res => {
        for (let key in res.data) {
          img.push(res.data[key].fileID);
        }
        this.setData({
          Img: img
        })
      }
    })

    var poem = JSON.parse(options.poem)
    console.log(poem);
    var choosed = options.choosedid.split(",")
    var list = []
    for (var i = 0; i < choosed.length; i++) {
      list.push(poem.content[choosed[i]])
    }

    this.setData({
      itemlist: list,
      title: poem.title,
      writer: poem.writer,
      theme: options.theme,
      fc: fc
    })
  },
  back: function () {
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  swiperChange: function (e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})